package com.honeyms.project.system.file.controller;

import java.io.IOException;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.honeyms.framework.aspectj.lang.annotation.Log;
import com.honeyms.framework.aspectj.lang.enums.BusinessType;
import com.honeyms.framework.config.HoneyMSConfig;
import com.honeyms.project.system.file.domain.File;
import com.honeyms.project.system.file.service.IFileService;
import com.honeyms.framework.web.controller.BaseController;
import com.honeyms.framework.web.domain.AjaxResult;
import com.honeyms.common.utils.file.FileUploadUtils;
import com.honeyms.common.utils.poi.ExcelUtil;
import com.honeyms.framework.web.page.TableDataInfo;

/**
 * 文件管理Controller
 * 
 * @author honeyms
 * @date 2020-12-13
 */
@Controller
@RequestMapping("/system/file")
public class FileController extends BaseController
{
    private String prefix = "system/file";

    @Autowired
    private IFileService fileService;

    @RequiresPermissions("system:file:view")
    @GetMapping()
    public String file()
    {
        return prefix + "/file";
    }

    /**
     * 查询文件管理列表
     */
    @RequiresPermissions("system:file:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(File file)
    {
        startPage();
        List<File> list = fileService.selectFileList(file);
        return getDataTable(list);
    }

    /**
     * 导出文件管理列表
     */
    @RequiresPermissions("system:file:export")
    @Log(title = "文件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(File file)
    {
        List<File> list = fileService.selectFileList(file);
        ExcelUtil<File> util = new ExcelUtil<File>(File.class);
        return util.exportExcel(list, "file");
    }

    /**
     * 新增文件管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存文件管理
     */
    @RequiresPermissions("system:file:add")
    @Log(title = "文件管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestParam("file") MultipartFile multipartFile, File file) throws IOException
    {
    	// 上传文件路径
    	String filePath = HoneyMSConfig.getUploadPath();
    	// 上传并返回新文件名称
    	String fileName = FileUploadUtils.upload(filePath, multipartFile);
    	file.setFileName(fileName);
    	file.setFilePath(filePath);
    	return toAjax(fileService.insertFile(file));
    }

    /**
     * 修改文件管理
     */
    @GetMapping("/edit/{fileId}")
    public String edit(@PathVariable("fileId") Long fileId, ModelMap mmap)
    {
        File file = fileService.selectFileById(fileId);
        mmap.put("file", file);
        return prefix + "/edit";
    }

    /**
     * 修改保存文件管理
     */
    @RequiresPermissions("system:file:edit")
    @Log(title = "文件管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(File file)
    {
        return toAjax(fileService.updateFile(file));
    }

    /**
     * 删除文件管理
     */
    @RequiresPermissions("system:file:remove")
    @Log(title = "文件管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fileService.deleteFileByIds(ids));
    }
}
